/*
 * Copyright (C) 2007-2010  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.wave;

import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import com.usoog.tdcore.pathfinder.PathLocation;

/**
 *
 * @author hylke
 */
public class PathLocationWaypoint implements PathLocation {

	private Point2D.Double location = new Point2D.Double(0, 0);
	private boolean endReached = false;
	private int lastWaypointId = 0;
	private double toGo = 0;
	private PathfinderWaypoints.Waypoint lastWaypoint;
	private PathfinderWaypoints.Waypoint nextWaypoint;
	private PathfinderWaypoints pf;
	/**
	 * Runs from 0 to 1000000 between two way points.
	 */
	private int waypointProgress = 0;
	private static int progressTarget = 1000000;

	public PathLocationWaypoint(PathfinderWaypoints pf) {
		this.pf = pf;
		lastWaypoint = pf.getWaypoint(lastWaypointId);
		nextWaypoint = pf.getWaypoint(lastWaypointId + 1);
		location.x = lastWaypoint.gamePoint.x;
		location.y = lastWaypoint.gamePoint.y;
		toGo = (pf.length() - lastWaypointId) * progressTarget - waypointProgress;
	}

	public double getGameLocationX() {
		return location.x;
	}

	public double getGameLocationY() {
		return location.y;
	}

	public Double getGameLocation(Double p) {
		if (p == null) {
			p = new Double(location.x, location.y);
			return p;
		}
		p.x = location.x;
		p.y = location.y;
		return p;
	}

	public boolean isEndReached() {
		return endReached;
	}

	public void setEndReached(boolean endReached) {
		this.endReached = endReached;
	}

	public double distanceFromEnd() {
		return toGo;
	}

	public int getLastWaypointId() {
		return lastWaypointId;
	}

	public void setLastWaypointId(int lastWaypointId) {
		this.lastWaypointId = lastWaypointId;
	}

	public int getWaypointProgress() {
		return waypointProgress;
	}

	public void setWaypointProgress(int waypointProgress) {
		this.waypointProgress = waypointProgress;
	}

	public void update(double speed) {
		waypointProgress += progressTarget * speed;
		if (waypointProgress > progressTarget) {
			waypointProgress -= progressTarget;
			lastWaypointId++;
			lastWaypoint = nextWaypoint;
			nextWaypoint = pf.getWaypoint(lastWaypointId + 1);
			if (nextWaypoint == null && !endReached) {
				endReached = true;
				location.x = lastWaypoint.gamePoint.x;
				location.y = lastWaypoint.gamePoint.y;
				return;
			}
		}
		interpolate();
	}

	private void interpolate() {
		if (endReached) {
			location.x = lastWaypoint.gamePoint.x;
			location.y = lastWaypoint.gamePoint.y;
		} else {
			double fraction = ((double) waypointProgress) / progressTarget;
			location.x = lastWaypoint.gamePoint.x + (nextWaypoint.gamePoint.x - lastWaypoint.gamePoint.x) * fraction;
			location.y = lastWaypoint.gamePoint.y + (nextWaypoint.gamePoint.y - lastWaypoint.gamePoint.y) * fraction;
		}
	}

	/**
	 * Rewind the progress for distance waypoints.
	 * @param distance the number of waypoints to rewind.
	 */
	public void rewind(double distance) {
		double floor = Math.floor(distance);
		waypointProgress -= progressTarget * (distance - floor);
		lastWaypointId -= floor;
		while (waypointProgress < 0) {
			waypointProgress += progressTarget;
			lastWaypointId--;
		}
		if (lastWaypointId < 0) {
			lastWaypointId = 0;
			waypointProgress = 0;
		}
		lastWaypoint = pf.getWaypoint(lastWaypointId);
		nextWaypoint = pf.getWaypoint(lastWaypointId + 1);
		interpolate();
	}
}
